package com.siecom.special;

import android.util.Log;
import com.siecom.framework.appinterface.APDU_RESP;
import com.siecom.framework.appinterface.APDU_SEND;
import com.siecom.framework.appinterface.Api;
import com.siecom.tools.ByteTool;
import com.siecom.tools.Timer;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


public class KSshimingka {
	private static final String TAG = "KSshimingka";
	private static KSshimingka module = new KSshimingka();
	private Api api;

	private KSshimingka() {
		api = new Api();

	}

	public static KSshimingka getInstance() {

		return module;

	}

	/**
	 * 圈存初始化 IC
	 *
	 * @param cipherFlag
	 * @param amount
	 * @param terminalCode
	 * @return
	 */
	public int InitForLoad(String cipherFlag, String amount, String terminalCode) {
		try {
			byte[] ATR = new byte[100];
			int ret = api.Icc_Open((byte) 0x00, (byte) 0x01, ATR);
			if (ret != 0) {
				Log.e("ret", "Icc_Open: ret = " + ret);
				return ret;
			}
			byte[] cmd = ByteTool.hexStr2Bytes("00A40000");
			byte[] sendData = ByteTool.hexStr2Bytes("3F00");
			short Lc = (short) sendData.length;
			short Le = 256;
			APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			APDU_RESP ApduResp = new APDU_RESP();
			ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			if (ret != 0) {
				Log.e("ret", "selectpath: ret = " + ret);
				return ret;
			}
			byte[] bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
			if (ApduResp.SWA == (byte) 0x90 && ApduResp.SWB == (byte) 0x00) {
				cmd = ByteTool.hexStr2Bytes("00B08500");
				sendData = ByteTool.hexStr2Bytes("");
				Lc = 0x00;
				Le = 256;
				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
				ApduResp = new APDU_RESP();
				ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
				Log.e("ret:", ret + "!!");
				bf = new byte[ApduResp.LenOut];
				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
				Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
				Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
				String sRet = ByteTool.byte2hex(bf);
				Log.e("sRet", sRet);
				String cityCode = sRet.substring(4, 8);
				Log.d(TAG, "城市代码=================>" + cityCode);
				String cityCardNo = sRet.substring(24, 40);
				Log.d(TAG, "市民卡号=================>" + cityCardNo);
			} else {
				return -9;
			}

			cmd = ByteTool.hexStr2Bytes("00A40000");
			sendData = ByteTool.hexStr2Bytes("1001");
			Lc = 0x02;
			Le = 256;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			Log.e("ret:", ret + "!!");
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));

			if (ApduResp.SWA == (byte) 0x90 && ApduResp.SWB == (byte) 0x00) {
				String msg = cipherFlag + amount + terminalCode;
				cmd = ByteTool.hexStr2Bytes("80500002");
				sendData = ByteTool.hexStr2Bytes(msg);
				Log.e(TAG, sendData.length + "!!");
				Lc = 0x0B;
				Le = 0x10;
				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
				ApduResp = new APDU_RESP();
				ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
				if (ret != 0) {
					Log.e(TAG, "Icc_Command: ret = " + ret);
					return -8;

				}
				bf = new byte[ApduResp.LenOut];
				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
				Log.e("bf", ByteTool.byte2hex(bf));
				Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
				Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
				if (ApduResp.SWA != (byte) 0x90 || ApduResp.SWB != (byte) 0x00) {
					Log.e(TAG, "Icc_Command fail: ret = " + ret);
					return -7;
				}
			} else {
				return -9;
			}
		} catch (Exception e) {
			api.Icc_Close((byte) 0);
		}
		return 0;
	}

	/**
	 * 圈存写卡 IC
	 *
	 * @param date
	 * @param time
	 * @param mac
	 * @return
	 */
	public int CreditForLoad(String date, String time, String mac) {
		try {
			String msg = date + time + mac;
			byte[] cmd = ByteTool.hexStr2Bytes("80520000");
			byte[] sendData = ByteTool.hexStr2Bytes(msg);
			Log.e(TAG, sendData.length + "!!");
			short Lc = 0x0B;
			short Le = 0x04;
			APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			APDU_RESP ApduResp = new APDU_RESP();
			int ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			if (ret != 0) {
				Log.e(TAG, "Icc_Command: ret = " + ret);
				return ret;
			}
			byte[] bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
			if (ApduResp.SWA != (byte) 0x90 || ApduResp.SWB != (byte) 0x00) {
				return -1;
			}
		} finally {
			api.Icc_Close((byte) 0x00);
		}
		return 0;
	}

	/**
	 * 非接式读取
	 *
	 * @return
	 */
	public JSONObject getCardInfoForPICC() {
		JSONObject icData = new JSONObject(); // ic卡信息
		try {
			int ret = api.Picc_Open();
			if (ret != 0) {
				Log.e("ret", "PIcc_Open: ret = " + ret);
				icData.put("ret", ret);
				return icData;
			}
			// 这里是检测卡片

			byte[] uid = new byte[10];
			while (api.Picc_Check((byte) 'A',uid) != 0) {
				// 这里要做超时
				new Timer(20 * 1000).start();
			}
			byte[] cmd = ByteTool.hexStr2Bytes("00A40400");
			byte[] sendData = ByteTool.hexStr2Bytes("A000000333010101");
			short Lc = 0x08;
			short Le = 256;
			APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			APDU_RESP ApduResp = new APDU_RESP();
//			ret = api.Picc_Command(ApduSend, ApduResp);
//			if (ret != 0) {
//				icData.put("ret", ret);
//				return icData;
//			}
			byte[] bf = new byte[ApduResp.LenOut];
//			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
//			Log.e(TAG, "====1=="+ByteTool.byte2hex(bf));
//			if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
//				cmd = ByteTool.hexStr2Bytes("00B20114");
//				sendData = ByteTool.hexStr2Bytes("");
//				Lc = 0x00;
//				Le = 256;
//				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
//				ApduResp = new APDU_RESP();
//				ret = api.Picc_Command(ApduSend, ApduResp);
//				if (ret != 0) {
//					icData.put("ret", ret);
//					return icData;
//				}
//				bf = new byte[ApduResp.LenOut];
//				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
//				Log.e(TAG, "ByteTool.byte2hex(bf)====2=="+ByteTool.byte2hex(bf));
//				String cardNo = ByteTool.byte2hex(bf).substring(8, 27);
//				icData.put("cardNo", cardNo);
//			} else {
//				icData.put("ret", -9);
//				return icData;
//			}
			cmd = ByteTool.hexStr2Bytes("00A40000");
			sendData = ByteTool.hexStr2Bytes("3F00");
			Lc = 0x02;
			Le = 256;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Picc_Command(ApduSend, ApduResp);
			Log.e("ret:",ret+"==");
			if (ret != 0) {
				icData.put("ret", ret);
				return icData;
			}
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e(TAG, "====3=="+ByteTool.byte2hex(bf));
			icData.put("ret", 0);
			if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
				cmd = ByteTool.hexStr2Bytes("00B08500");
				sendData = ByteTool.hexStr2Bytes("");
				Lc = 0x00;
				Le = 256;
				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
				ApduResp = new APDU_RESP();
				ret = api.Picc_Command(ApduSend, ApduResp);
				Log.e(TAG, "====1=ret="+ret);
				if (ret != 0) {
					icData.put("ret", ret);
					return icData;
				}
				bf = new byte[ApduResp.LenOut];
				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
				String sRet = ByteTool.byte2hex(bf);
				Log.e(TAG, "====1=="+ByteTool.byte2hex(bf));
				String cityCode = sRet.substring(4, 8);
				icData.put("cityCode", cityCode);
				if (!"2153".equals(cityCode)) {
					Log.d(TAG, "外地市民卡不能充值");
				}
				String cityCardNo = sRet.substring(24, 40);
				icData.put("metroCardNo", cityCardNo);
			} else {
				icData.put("ret", -9);
				return icData;
			}
			cmd = ByteTool.hexStr2Bytes("00A40000");
			sendData = ByteTool.hexStr2Bytes("1001");
			Lc = 0x02;
			Le = 256;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Picc_Command(ApduSend, ApduResp);
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e(TAG, "====4=="+ByteTool.byte2hex(bf));
			if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
				cmd = ByteTool.hexStr2Bytes("00B09500");
				sendData = ByteTool.hexStr2Bytes("");
				Lc = 0x00;
				Le = 256;
				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
				ApduResp = new APDU_RESP();
				ret = api.Picc_Command(ApduSend, ApduResp);
				bf = new byte[ApduResp.LenOut];
				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
				String sRet = ByteTool.byte2hex(bf);
				Log.e(TAG, "====5=="+ByteTool.byte2hex(bf));
				if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
					icData.put("Validity", sRet.substring(40, 56));
				} else {
					icData.put("ret", -9);
					return icData;
				}
			} else {
				icData.put("ret", -9);
				return icData;
			}
			cmd = ByteTool.hexStr2Bytes("805C0002");
			sendData = ByteTool.hexStr2Bytes("");
			Lc = 0x00;
			Le = 0x04;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Picc_Command(ApduSend, ApduResp);
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
				int cash = ByteTool.byteArray2int(bf);
				float temp = cash / 100f;
				icData.put("Balance", Float.toString(temp));
			} else {
				icData.put("ret", -9);
				return icData;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			api.Picc_Close();
		}
		return icData;
	}
    /**
     * 圈存初始化非接
     *
     * @param cipherFlag
     * @param amount
     * @param terminalCode
     * @return
     */
    public JSONObject PICInitForLoad(String cipherFlag, String amount, String terminalCode) {
    	Log.i(TAG, "PICInitForLoad================");
    	JSONObject json = new JSONObject();
        try {
            int ret = api.Picc_Open();
            if (ret != 0) {
            	json.put("ret", ret);
				return json;
            }
            //这里是检测卡片
            while (api.Picc_Check((byte) 'A') != 0) {
                //这里要做超时
            }
            byte[] cmd = ByteTool.hexStr2Bytes("00A40000");
            byte[] sendData = ByteTool.hexStr2Bytes("1001");
            short Lc = 0x02;
            short Le = 256;
            APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
            APDU_RESP ApduResp = new APDU_RESP();
            ret = api.Picc_Command(ApduSend, ApduResp);
            if (ret != 0) {
            	json.put("ret", ret);
				return json;
            }
            byte[] bf = new byte[ApduResp.LenOut];
            System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
            if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
                String msg = cipherFlag + amount + terminalCode;
                cmd = ByteTool.hexStr2Bytes("80500002");
                sendData = ByteTool.hexStr2Bytes(msg);
                Lc = 0x0B;
                Le = 0x10;
                ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
                ApduResp = new APDU_RESP();
                ret = api.Picc_Command(ApduSend, ApduResp);
                if (ret != 0) {
					json.put("ret", -8);
					return json;

                }
                bf = new byte[ApduResp.LenOut];
                System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
                Log.e(TAG, ByteTool.byte2hex(bf));
                Log.e(TAG, ByteTool.byteToHexString(ApduResp.SWA));
                Log.e(TAG, ByteTool.byteToHexString(ApduResp.SWB));
                if (ApduResp.SWB == (byte) 0x90 && ApduResp.SWA == (byte) 0x00) {
                    Log.e(TAG, "Icc_Command fail: ret = " + ret);
                    json.put("ret", 0);
					json.put("dataOut",
							ByteTool.byte2hex(bf));
					return json;
                }
            } else {
            	json.put("ret", -9);
				return json;
            }
        } catch (Exception e) {
            api.Picc_Close();
        }
        return json;
    }

	/**
	 * 圈存写卡非接
	 *
	 * @param date
	 * @param time
	 * @param
	 * @return
	 */
	public JSONObject PICCreditForLoad(String date, String time, String mac2) {
		JSONObject jsonStr = new JSONObject();
		Log.e(TAG, "PICCreditForLoad=====");
		try {
			String msg = date + time + mac2;
			Log.e(TAG, "PICCreditForLoad===msg=="+msg);
			byte[] cmd = ByteTool.hexStr2Bytes("80520000");
			byte[] sendData = ByteTool.hexStr2Bytes(msg);
			Log.e(TAG, sendData.length + "!!");
			short Lc = 0x0B;
			short Le = 0x04;
			APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			APDU_RESP ApduResp = new APDU_RESP();
			int ret = api.Picc_Command(ApduSend, ApduResp);
			Log.e(TAG, "Icc_Command: ret = " + ret);
			if (ret != 0) {
				jsonStr.put("ret", ret);
				return jsonStr;
			}
			byte[] bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e(TAG,  "Icc_Command: bf===1= "+ByteTool.byte2hex(bf));
			Log.e(TAG, ByteTool.byteToHexString(ApduResp.SWA));
			Log.e(TAG, ByteTool.byteToHexString(ApduResp.SWB));
			if (ApduResp.SWB == (byte) 0x90 || ApduResp.SWA == (byte) 0x00) {
				Log.e(TAG, "Icc_Command fail: ret = " + ret);
				jsonStr.put("ret", ret);
				jsonStr.put("dataOut",ByteTool.byte2hex(bf)+"9000");
				return jsonStr;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			api.Picc_Close();
		}
		return jsonStr;
	}

	/**
	 * 圈存初始化成功  写卡流程失败  非接
	 *
	 * @return
	 */
	public void PICCreditForLoadClose() {
		api.Picc_Close();
	}


	/**
	 * 接触式IC读取
	 *
	 * @return
	 */
	public int getCardInfoForIc() {
		try {
			byte[] ATR = new byte[100];
			int ret = api.Icc_Open((byte) 0x00, (byte) 0x01, ATR);
			if (ret != 0) {
				Log.e("ret", "Icc_Open: ret = " + ret);
				return ret;
			}
			byte[] cmd = ByteTool.hexStr2Bytes("00A40000");
			byte[] sendData = ByteTool.hexStr2Bytes("3F00");
			short Lc = (short) sendData.length;
			short Le = 256;
			APDU_SEND ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			APDU_RESP ApduResp = new APDU_RESP();
			ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			if (ret != 0) {
				Log.e("ret", "selectpath: ret = " + ret);
				return ret;
			}
			byte[] bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
			if (ApduResp.SWA == (byte) 0x90 && ApduResp.SWB == (byte) 0x00) {
				cmd = ByteTool.hexStr2Bytes("00B08500");
				sendData = ByteTool.hexStr2Bytes("");
				Lc = 0x00;
				Le = 256;
				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
				ApduResp = new APDU_RESP();
				ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
				Log.e("ret:", ret + "!!");
				bf = new byte[ApduResp.LenOut];
				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);

				Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
				Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
				String sRet = ByteTool.byte2hex(bf);
				Log.e("sRet", sRet);
				String cityCode = sRet.substring(4, 8);
				Log.d(TAG, "城市代码=================>" + cityCode);
				if (!"2153".equals(cityCode)) {
					Log.d(TAG, "外地市民卡不能充值");
				}
				String cityCardNo = sRet.substring(24, 40);
				Log.d(TAG, "市民卡号=================>" + cityCardNo);
			} else {
				return -9;
			}

			cmd = ByteTool.hexStr2Bytes("00A40000");
			sendData = ByteTool.hexStr2Bytes("1001");
			Lc = 0x02;
			Le = 256;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			Log.e("ret:", ret + "!!");
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));

			if (ApduResp.SWA == (byte) 0x90 && ApduResp.SWB == (byte) 0x00) {
				cmd = ByteTool.hexStr2Bytes("00B09500");
				sendData = ByteTool.hexStr2Bytes("");
				Lc = 0x00;
				Le = 256;
				ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
				ApduResp = new APDU_RESP();
				ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
				Log.e("ret:", ret + "!!");
				bf = new byte[ApduResp.LenOut];
				System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);

				Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
				Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
				String sRet = ByteTool.byte2hex(bf);
				Log.e("sRet", sRet);
				String expDate = sRet.substring(40, 56);
				Log.d(TAG, "市民卡有效期============>" + expDate);

			} else {
				return -9;
			}

			cmd = ByteTool.hexStr2Bytes("805C0002");
			sendData = ByteTool.hexStr2Bytes("");
			Lc = 0x00;
			Le = 0x04;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			Log.e("ret:", ret + "!!");
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));
			if (ApduResp.SWA == (byte) 0x90 && ApduResp.SWB == (byte) 0x00) {
				int cash = ByteTool.byteArray2int(bf);
				float temp = cash / 100.0f;
				Log.d(TAG, "读取市民卡余额:" + temp + "元");
			} else {
				return -9;
			}

			cmd = ByteTool.hexStr2Bytes("00B201C4");
			sendData = ByteTool.hexStr2Bytes("");
			Lc = 0x00;
			Le = 0x17;
			ApduSend = new APDU_SEND(cmd, Lc, sendData, Le);
			ApduResp = new APDU_RESP();
			ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
			Log.e("ret:", ret + "!!");
			bf = new byte[ApduResp.LenOut];
			System.arraycopy(ApduResp.DataOut, 0, bf, 0, ApduResp.LenOut);
			Log.e("bf", ByteTool.byte2hex(bf));
			Log.e("SWA", ByteTool.byteToHexString(ApduResp.SWA));
			Log.e("SWB", ByteTool.byteToHexString(ApduResp.SWB));

			if (ApduResp.SWA == (byte) 0x90 && ApduResp.SWB == (byte) 0x00) {
				ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
				for (int i = 1; i <= 10; i++) {
					Log.d(TAG, "读取第" + i + "条记录");
					byte[] cmd_TradeNote = { (byte) 0x00, (byte) 0xB2,
							(byte) (0x000000ff & (i)), (byte) 0xC4 };
					sendData = ByteTool.hexStr2Bytes("");
					Lc = 0x00;
					Le = 0x17;
					ApduSend = new APDU_SEND(cmd_TradeNote, Lc, sendData, Le);
					ApduResp = new APDU_RESP();
					ret = api.Icc_Command((byte) 0x00, ApduSend, ApduResp);
					Log.e("ret:", ret + "!!");
					bf = new byte[ApduResp.LenOut];
					System.arraycopy(ApduResp.DataOut, 0, bf, 0,
							ApduResp.LenOut);

					String result2 = ByteTool.byte2hex(bf);

					Log.d(TAG, "result_TradeNotes_" + i + ":" + result2);

					HashMap<String, Object> map = new HashMap<String, Object>();
					// map.put("DateTime", result2.substring(32, 46));// 交易日期
					Date date = null;
					try {
						date = new SimpleDateFormat("yyyyMMddhhmmss")
								.parse(result2.substring(32, 46));
						String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
								.format(date);
						map.put("DateTime", now);// 交易日期
					} catch (ParseException e) {
						e.printStackTrace();
					}
					String money = result2.substring(8, 18);// 交易金额
					float amount = Integer.parseInt(money, 16) / 100.0f;

					String type = result2.substring(18, 20);// 交易类型
					String runningNum = result2.substring(20, 32);
					// 01为圈存 其他为消费
					if (type.equals("01")) {
						type = "圈存";
					} else {
						type = "消费";
					}
					map.put("Log", type + "[" + amount + "]" + "  交易流水号["
							+ runningNum + "]");

					Log.d(TAG, type + "[" + amount + "]" + "  交易流水号["
							+ runningNum + "]");
					listItem.add(map);

				}

			} else {
				return -9;
			}

		} finally {

			api.Icc_Close((byte) 0);
		}
		return 0;

	}

}
